<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Member extends Base
{
	public function Member()
	{	
		$this->title = $GLOBALS['lang']->GetLanguage("Member", "title");
		$this->toolbar = true;
		$this->moduleID = 100;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-user";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
		
		echo '	';
	}
	
	public function Add()
	{		
		include("./_tables.php");
		$panel = new Panel();
		
		if($this->error)
		{
			$panel->AddRow();
			$panel->AddText($this->errorText);
		}
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("username", $GLOBALS['lang']->GetLanguage("Member", "username"), $_POST['username'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("title", $GLOBALS['lang']->GetLanguage("Member", "strTitle"), $_POST['title'], 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("firstName", $GLOBALS['lang']->GetLanguage("Member", "firstName"), $_POST['firstName'], 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("lastName", $GLOBALS['lang']->GetLanguage("Member", "lastName"), $_POST['lastName'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("job", $GLOBALS['lang']->GetLanguage("Member", "job"), $_POST['job'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("street", $GLOBALS['lang']->GetLanguage("Member", "street"), $_POST['street'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("city", $GLOBALS['lang']->GetLanguage("Member", "city"), $_POST['city'], 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("dateOfWorkBegin", $GLOBALS['lang']->GetLanguage("Member", "dateOfWorkBegin"), $_POST['dateOfWorkBegin'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("email", $GLOBALS['lang']->GetLanguage("Member", "email"), $_POST['email'], 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("telephonenumber", $GLOBALS['lang']->GetLanguage("Member", "telephone"), $_POST['telephonenumber'], 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("birthday", $GLOBALS['lang']->GetLanguage("Member", "birthday"), $_POST['birthday'], 45, 30);
		$panel->AddRow();
		$panel->AddComboBox("countryID", $GLOBALS['lang']->GetLanguage("Member", "country"), $_POST['countryID'], $tblcountry, "id", "countryDesc");
		$panel->AddRow();
		$panel->AddComboBox("languageID", $GLOBALS['lang']->GetLanguage("Member", "language"), $_POST['languageID'],  $tbllang, "id", "langDesc");
		
		$panel->AddHidden("password", "");
		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "member");
		
		$panel->Close();
	}
	public function Edit()
	{		
		include("./_tables.php");
		$panel = new Panel();
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT *,DATE_FORMAT(dateOfWorkBegin,'%d.%m.%Y') as workBegin,DATE_FORMAT(birthday,'%d.%m.%Y') as birth FROM ".$tbluser." WHERE id=".$_POST['id'].""));
		
		if(isset($_POST['username']))
		{
			if($this->error)
			{
				$panel->AddRow();
				$panel->AddText($this->errorText);
			}
			
			$res->username = $_POST['username'];
			$res->firstName = $_POST['firstName'];
			$res->lastName = $_POST['lastName'];
			$res->street = $_POST['street'];
			$res->city = $_POST['city'];
			$res->telephonenumber = $_POST['telephonenumber'];
			$res->countryID = $_POST['countryID'];
			$res->languageID = $_POST['languageID'];
			$res->title = $_POST['title'];
			$res->job = $_POST['job'];
			$res->workBegin = $_POST['dateOfWorkBegin'];
			$res->email = $_POST['email'];
			$res->birth = $_POST['birthday'];
			
			
		}
		
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("username", $GLOBALS['lang']->GetLanguage("Member", "username"), $res->username, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("title", $GLOBALS['lang']->GetLanguage("Member", "strTitle"), $res->title, 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("firstName", $GLOBALS['lang']->GetLanguage("Member", "firstName"), $res->firstName, 45, 30);
		$panel->AddRow();
		$panel->AddMandatoryTextBox("lastName", $GLOBALS['lang']->GetLanguage("Member", "lastName"), $res->lastName, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("job", $GLOBALS['lang']->GetLanguage("Member", "job"), $res->job, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("street", $GLOBALS['lang']->GetLanguage("Member", "street"), $res->street, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("city", $GLOBALS['lang']->GetLanguage("Member", "city"), $res->city, 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("dateOfWorkBegin", $GLOBALS['lang']->GetLanguage("Member", "dateOfWorkBegin"), $res->workBegin, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("email", $GLOBALS['lang']->GetLanguage("Member", "email"), $res->email, 45, 30);
		$panel->AddRow();
		$panel->AddTextBox("telephonenumber", $GLOBALS['lang']->GetLanguage("Member", "telephone"), $res->telephonenumber, 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("birthday", $GLOBALS['lang']->GetLanguage("Member", "birthday"), $res->birth, 45, 30);
		$panel->AddRow();
		$panel->AddComboBox("countryID", $GLOBALS['lang']->GetLanguage("Member", "country"), $res->countryID,  $tblcountry, "id", "countryDesc");
		$panel->AddRow();
		$panel->AddComboBox("languageID", $GLOBALS['lang']->GetLanguage("Member", "language"), $res->languageID, $tbllang, "id", "langDesc");
		
		$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "member");
		$panel->AddHidden("id", $_POST['id']);
		
		$panel->Close();
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "activate")
		{
			$this->Activate();
		}
		else if($_POST['action'] == "recoverPassword")
		{
			$this->RecoverPassword();
			$this->Paint();
			
			Alert($GLOBALS['lang']->GetLanguage("Member", "passwordRecoverd"));
		}
		else if($_POST['action'] == "save")
		{
			
			if($this->CheckInput())
			{
				$_POST['password'] = hash("sha256", $_POST['username']);
				
				$_POST['dateOfWorkBegin'] = ConvertDate($_POST['dateOfWorkBegin'], ".");
				$_POST['birthday'] = ConvertDate($_POST['birthday'], ".");
				
				$GLOBALS['db']->SaveObject($tbluser);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			if($this->CheckInput())
			{
				$_POST['dateOfWorkBegin'] = ConvertDate($_POST['dateOfWorkBegin'], ".");
				$_POST['birthday'] = ConvertDate($_POST['birthday'], ".");
				
				$GLOBALS['db']->UpdateObject($tbluser);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tbluser);
			$this->Paint();
		}
		else if($_GET['type'] == "iframe" && $_GET['action'] == "paintcp")
		{
			$this->PaintChangePassword();
		}
		else
		{
			$this->Paint();
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		
		if($_POST['username'] == "" || $_POST['firstName'] == "" || $_POST['lastName'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		$query = "SELECT * FROM ".$tbluser." WHERE username='".$_POST['username']."'";
		
		if($GLOBALS['db']->Query($query)->num_rows)
		{
			if($_POST['action'] == "update")
			{
				$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
				
				if($_POST['id'] == $res->id)
				{
					return true;
				}
			}
			$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
			$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Member", "username"), $string);
			$string = str_replace("{1}", $_POST['username'], $string);

			$this->errorText = $string;
			return false;
		}
		
		return true;
	}
	
	public function RecoverPassword()
	{
		include("./_tables.php");
		
		$query = "SELECT * FROM ".$tbluser." WHERE id='".$_POST['id']."'";
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
		
		$password = hash("sha256", $res->username);
		
		$query = "UPDATE ".$tbluser." SET password='".$password."' WHERE id='".$_POST['id']."'";
		$GLOBALS['db']->Query($query);
		
	}
	public function Activate()
	{
		include("./_tables.php");
		
		$query = "SELECT * FROM ".$tbluser." WHERE id='".$_POST['id']."'";
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));
		
		
		if($res->activated == "1")
		{
			$activate = "0";
		}
		else
		{
			$activate = "1";
		}
		
		$query = "UPDATE ".$tbluser." SET activated='".$activate."' WHERE id='".$_POST['id']."'";
		$GLOBALS['db']->Query($query);
		
		$this->Paint();
	}

	public function CheckFastEditInput()
	{
		include("./_tables.php");
		
		if($_GET['field'] == "username")
		{
			$query = $GLOBALS['db']->Query("SELECT * FROM ".$tbluser." WHERE username='".$_GET['value']."'");
			
			if($GLOBALS['db']->NumRow($query))
			{
				$res = $GLOBALS['db']->Fetch($query);
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Member", "username"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
					
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=member');
					//</script>";
					return false;
				}
			}
		}
		
		return true;
	}
	
	public function Paint()
	{
		include("./_tables.php");
		
		echo 
		'<div id="cpanel">';
				
		$dgv = new DataGridView($this);
		$dgv->AddTextFilter($this->filter);
		
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "username"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "firstName"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "lastName"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "street"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "city"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Member", "telephone"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "activate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "password"), "17%", true);
		$dgv->CloseHeader();
		
		if($this->filter != "")
		{	
			$query = "SELECT * FROM ".$tbluser." WHERE username LIKE '%".$this->filter."%' OR firstName LIKE '%".$this->filter."%' OR lastName LIKE '%".$this->filter."%'";
			$res = $GLOBALS['db']->Query($query);
		}
		else
		{
			$res = $GLOBALS['db']->Query("SELECT * FROM ".$tbluser);
		}
		
		if($this->permission->editPermission)
			$title = "fastEdit";
		else
			$title = "";
		
		$count = 0;
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->username, $title, "", "username");
			$dgv->AddData($line->firstName, $title, "", "firstName");
			$dgv->AddData($line->lastName, $title, "", "lastName");
			$dgv->AddData($line->street, $title, "", "street");
			$dgv->AddData($line->city, $title, "", "city");
			$dgv->AddData($line->telephonenumber, $title, "", "telephonenumber");
			
			if($this->permission->editPermission)
				$dgv->AddData($line->activated, "bool-fastEdit", "", "activated");
			else
				$dgv->AddData($line->activated, "bool", "", "activated");
			
			if($this->permission->editPermission)
			{
				$img = '<img src="images/password.png" border="0" height="15px" width="15px" alt="recoverPassword" 
								onmouseover="return overlib(\''.$GLOBALS['lang']->GetLanguage("Member", "recoverPassword").'\');" onmouseout="return nd();"/>';
				$dgv->AddMisc("", 'return ListItemTask(\'cb'.$count.'\',\'recoverPassword\');', $img);
			}	
			
			$dgv->CloseRow();
			$count++;
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';
	}
	public function PaintChangePassword()
	{
		echo '
		
		<div id="changePasswordTable" style="margin-left:25%;margin-top:25%;">
			<table>
				<tr>
					<td>
						'.$GLOBALS['lang']->GetLanguage("Member","actualPassword").':
					</td>
					<td>
						<input type="text" name="actualPassword value="" />
					</td>
				</tr>
				<tr>
					<td>
							'.$GLOBALS['lang']->GetLanguage("Member","newPassword").':
					</td>
					<td>
						<input type="text" name="newPassword1" value="" />
					</td>
				</tr>
				<tr>
					<td>
							'.$GLOBALS['lang']->GetLanguage("Member","newPasswordRe").':
					</td>
					<td>
						<input type="text" name="newPassword2" value="" />
					</td>
				</tr>
				<tr>
					<td>
						<input type="button" name="submit" value="'.$GLOBALS['lang']->GetLanguage("Common","save").'" onclick="" />
					</td>
				</tr>
			</table>
		</div>
		
		';
	}
	
	public function Toolbar()
	{
		if(!$this->allowed)
			return;
			
		PaintDefaultToolbar($this);
	}

	public function AddCSS()
	{
		echo '<link rel="stylesheet" type="text/css" href="css/SqueezeBox/squeeze.css" /> ';
	}
	public function AddJS()
	{
		echo '<script type="text/javascript">
		
		window.addEvent(\'domready\', function() {

			SqueezeBox.initialize({});
			SqueezeBox.assign($$(\'a.modal\'), {
				parse: \'rel\'
			});
		});
		
		</script>';
	}
}

?>